package com.topboya.linker.ui.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Parcelable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.topboya.linker.R;
import com.topboya.linker.model.response.CustomerByOrgIdAndNewlyCusListResopnse;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.DictByKeyResponse;
import com.topboya.linker.model.response.NewlyOrgListResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.adapter.FlowdepTagAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.CustomerPresenter;
import com.topboya.linker.ui.view.CustomerView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class SelectCustomerActivity extends BaseActivity<CustomerView, CustomerPresenter> implements CustomerView {

    private static final String TAG = "SelectCustomerActivity:";
    private TextView toolbar_title;
    private TextView tv_device_name;
    private EditText et_add_new_device;
    private TextView tv_add_new_department;
    private TextView tv_add;
    private LinearLayout ll_depart_empty;
    private Button  btn_select;
    private OrganizationResopnse.DocDetailInfoList docDetailInfo;
    private TagFlowLayout id_flowlayout;
    private List<DictByKeyResponse.Data> data;
    private String orgID;
    private ArrayList<String> DepartnameList;
    private String departmentvalue;
    private List<String> listdepart;
    private FlowdepTagAdapter  flowdepTagAdapter;
    private ArrayList<String> selectepartnameList;
    private Set<Integer>  set;
    private List<CustomerByOrgIdResponse.DepartmentList> departmentList;
    @Override
    protected int getLayoutId() {

        return R.layout.activity_select_device;
    }

    @Override
    protected void initView() {

        toolbar_title = findViewById(R.id.toolbar_title);
        tv_device_name = findViewById(R.id.tv_device_name);
        tv_add_new_department = findViewById(R.id.tv_add_new_department);
        tv_add = findViewById(R.id.tv_add);
        ll_depart_empty = findViewById(R.id.ll_depart_empty);
        btn_select = findViewById(R.id.btn_select);
        id_flowlayout = findViewById(R.id.id_flowlayout);
        et_add_new_device = findViewById(R.id.et_add_new_device);

    }

    @Override
    protected void initData() {

        toolbar_title.setText("选择客户");
        docDetailInfo = (OrganizationResopnse.DocDetailInfoList) getIntent().getSerializableExtra("DocDetailInfo");
        LogUtils.v(TAG + "docDetailInfo:"+docDetailInfo.toString());
        String devicesname = "<font color=\"#AAAAAA\">"+"终端名称："+"</font>"+"<font color=\"#000000\">"+docDetailInfo.getOrgName()+"</font>";
        tv_device_name.setText(Html.fromHtml(devicesname));
        listdepart  = new ArrayList<>();
//        存放被选择的客户
        selectepartnameList = new ArrayList<>();
        flowdepTagAdapter = new FlowdepTagAdapter(this,id_flowlayout,listdepart);
        id_flowlayout.setAdapter(flowdepTagAdapter);

        mPresenter.getCustomerByOrgId(docDetailInfo.getId(),true);
//        mPresenter.getDictByKey("hos_department",null,true);
    }

    @Override
    protected void initListener() {

        tv_add_new_department.setOnClickListener(v -> {

             if (null ==data){

                 mPresenter.getDictByKey("hos_department",null,true,null);
             }else {

                 OptionsPickerView pvOptions = new OptionsPickerBuilder(this, (options1, option2, options3, v1) -> {

                     String departname = DepartnameList.get(options1);
                     LogUtils.v(TAG + "departname:"+departname);
                     orgID =docDetailInfo.getId();
                     departmentvalue = data.get(options1).getValue();
                     tv_add_new_department.setText(departname);

                 }).setCyclic(true,true,true)
                         .setTextColorCenter(Color.BLUE)
                         .build();
                 pvOptions.setPicker(DepartnameList , null, null);
                 pvOptions.show();

             }

        });
        tv_add.setOnClickListener(v -> {

           String  cusName = et_add_new_device.getText().toString().trim();

           if (TextUtils.isEmpty(cusName) || TextUtils.isEmpty(orgID)){

               UIUtils.showToast("请先填写完成信息");

           }else {

               mPresenter.createDoctorCustomer(orgID,null,cusName,null,departmentvalue,null,null);

           }

        });

        id_flowlayout.setOnSelectListener(selectPosSet -> {

            LogUtils.v("choose:" + selectPosSet.toString());
            int size =selectPosSet.size();
            btn_select.setText("选择（"+size+"）");
            set =selectPosSet;

            }
        );
        btn_select.setOnClickListener(v -> {

            LogUtils.v(TAG +"set.toString():"+set.toString());
            selectepartnameList.clear();
            for (Integer select : set) {

                selectepartnameList.add(listdepart.get(select));

            }
            Intent intent = new Intent(this,CreateTaskActivity1.class);
            intent.putExtra("DocDetailInfo",docDetailInfo);
            intent.putStringArrayListExtra("selectepartnameList",  selectepartnameList);
            intent.putExtra("departmentList", (Serializable)departmentList);
            startActivity(intent);

        });
    }

    @Override
    protected CustomerPresenter createPresenter() {

        return new CustomerPresenter(this);
    }



    @Override
    public void setCustomeData(ArrayList<OrganizationResopnse.DocDetailInfoList> groupHList, ArrayList<NewlyOrgListResponse.Data> groupHListNewly) {

    }

    @Override
    public void setDepartment( List<CustomerByOrgIdResponse.DepartmentList> departmentlist) {

        this.departmentList = departmentlist;
        if (null == departmentlist){

            id_flowlayout.setVisibility(View.GONE);
            ll_depart_empty.setVisibility(View.VISIBLE);
        }else {

            id_flowlayout.setVisibility(View.VISIBLE);
            ll_depart_empty.setVisibility(View.GONE);
            listdepart.clear();
            for(int i=0; i <departmentlist.size(); i++){

                listdepart.add(departmentlist.get(i).getCusName());
                flowdepTagAdapter.notifyDataChanged();
            }
        }

    }

    @Override
    public void setDictByKey(List<DictByKeyResponse.Data> data) {

        this.data = data;
        DepartnameList = new ArrayList<>();
        for (int i =0;i<data.size();i ++){

            DepartnameList.add(data.get(i).getLabel()) ;
        }
    }

    @Override
    public void createCustomer() {

//        添加成功，刷新flowlayout
      mPresenter.getCustomerByOrgId(docDetailInfo.getId(),true);


    }

    @Override
    public void setDepartVisitor(HashMap<String, List<CustomerByOrgIdAndNewlyCusListResopnse.CusListChild>> departmentgroupmap, List<CustomerByOrgIdAndNewlyCusListResopnse.NewlyCusList> newlyCusLists) {

    }



}
